package com.aspire.moss.portal.desktop;

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.WeakHashMap;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * 图标缓存
 * 
 * @author Administrator
 * 
 */
public class IconCache {

    private static final WeakHashMap<String, SoftReference<Bitmap>> ICONCACHE = new WeakHashMap<String, SoftReference<Bitmap>>(0);

    public interface IconCallback {
        public void iconLoadFinish(Bitmap iconDrawable);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            if (null != msg.obj && msg.obj instanceof AsynIconLoader) {
                final AsynIconLoader loader = (AsynIconLoader) msg.obj;
                loader.callback.iconLoadFinish(loader.bitmap);
            }
        }
    };

    public static final IconCache instance = new IconCache();

    private IconCache() {

    }

    public static IconCache getInsance() {

        return instance;
    }

    public Bitmap loadDrawable(Context mContext, final String path, final IconCallback callback) {
        Log.d("IconCache", "path=" + path + " callback=" + callback);
        if (ICONCACHE.containsKey(path)) {
            SoftReference<Bitmap> softReference = ICONCACHE.get(path);
            Bitmap drawable = softReference.get();
            if (drawable != null) {
                return drawable;
            }
        } else {
            handler.post(new AsynIconLoader(mContext, path, handler, callback));
        }
        return null;
    }

    /**
     * 读取图片的,以后可能要重写
     * 
     * @param path
     * @return
     * @throws IOException
     */
    public static Bitmap loadIconFormPath(Context mContext, String path) {
        Log.i("IconCache", "loadIconFormPath path=" + path);
        AssetManager manager = mContext.getAssets();
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(manager.open(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    private static final class AsynIconLoader extends Thread {

        private String path;

        private Handler handler;

        private IconCallback callback;

        private Bitmap bitmap;

        private Context mContext;

        public AsynIconLoader(Context mContext, String path, Handler handler, IconCallback callback) {
            super();
            this.mContext = mContext;
            this.path = path;
            this.handler = handler;
            this.callback = callback;
        }

        @Override
        public void run() {
            Log.d("IconCache", "AsynIconLoader path=" + path);
            bitmap = loadIconFormPath(mContext, path);
            ICONCACHE.put(path, new SoftReference<Bitmap>(bitmap));
            Message message = handler.obtainMessage(0, this);
            handler.sendMessage(message);
        }
    }
}
